<template>
  <div>
    <div class="box">
      <div class="boxs">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item prop="username">
            <i class="el-icon-user-solid"></i>
            <el-input v-model="ruleForm.username" class="inp"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <i class="el-icon-lock"></i>
            <el-input v-model="ruleForm.password" class="inps"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >立即创建</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入用户密码", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post("login", this.ruleForm).then((res) => {
            console.log(res);
            if (res.data.meta.status == 200) {
              this.$message({
                message: "恭喜你，这是一条成功消息",
                type: "success",
              });
              sessionStorage.setItem('token',res.data.data.token)
              this.$router.push({path:'/'})
            }else{
                 this.$message.error('错了哦，这是一条错误消息');
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang="scss" scoped>
.box {
  width: 100%;
  height: 500px;
  background-color: #344f6a;
  padding-top: 200px;
}
.boxs {
  width: 500px;
  height: 300px;
  background-color: #fff;
  margin: auto auto;
}
.el-icon-user-solid {
  position: fixed;
  top: 240px;
  left: 530px;
  // float: left;
}
.inp {
  width: 460px;
  margin-left: -80px;
  margin-top: 20px;
}
.el-icon-lock {
  position: fixed;
  top: 300px;
  left: 530px;
}
.inps {
  width: 460px;
  margin-left: -80px;
  // margin-top: 20px;
}
</style>